<?php
require_once dirname(dirname(__FILE__)) .'/cse-init.php';

$config = Cinnamon::registry('config');

if ( $_GET['action'] == 'upgrade' ) {
	$db = Cinnamon::registry('db');
	$config = Cinnamon::registry('config');
	
	if ( !$config->exists('cse.version') ) {
		// fix for svn users :)
		$config->add('cse.version', '0.4', 'y');
	}
	
	switch ( $config->get('cse.version') ) {
		case '0.4':
			$db->query("ALTER TABLE ". $db->sites ." ADD ignore_www ENUM('y','n') NOT NULL DEFAULT 'y' AFTER site_name");
			$db->query("ALTER TABLE ". $db->sites ." ADD match_subdomains ENUM('y','n') NOT NULL DEFAULT 'n' AFTER ignore_www");				
		case '0.5':
			$db->query("ALTER TABLE ". $db->sites ." ADD has_code ENUM('y', 'n') DEFAULT 'n' NOT NULL");
		case '0.5.7':
			$db->query("ALTER TABLE ". $db->visits ." "
					."CHANGE visit_id request_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, "
					."CHANGE visit_datetime request_datetime DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', "
					."CHANGE visit_utc_datetime request_utc_datetime DATETIME NOT NULL, "
					."CHANGE session_id visit_id VARCHAR(40) NOT NULL"
			);
			$db->query("ALTER TABLE ". $db->visits ." ADD request_type VARCHAR(20) NOT NULL AFTER request_id");
			$db->query("RENAME TABLE ". $db->visits ." TO ". $db->requests);
			$db->query("UPDATE ". $db->requests ." SET request_type='page'");
		default:
			$config->set('cse.version', Cinnamon::getVersion());
			break;
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upgrade | the Cinnamon</title>
<link rel="stylesheet" href="../view/screen.css" type="text/css" media="screen" />
</head>

<body class="dialog">
	<div id="container">
		<div id="header"><span>the Cinnamon</span></div>
		<div class="block">
<?php if ( strcmp($_GET['action'], 'upgrade') == 0 ): ?>
		<h1>Upgrade completed!</h1>
		<p>That&rsquo;s all! Your database has been upgraded.</p>
		<p class="buttons"><button onclick="location.href='<?php echo urldecode($_GET['url']); ?>'">Back to Cinnamon</button></p>
<?php elseif ( strcmp(Cinnamon::getVersion(), $config->get('cse.version')) <= 0 ) : ?>
		<h1>Version <?php echo Cinnamon::getVersion(); ?></h1>
<?php else: ?>
		<h1>Upgrade to <?php echo Cinnamon::getVersion(); ?></h1>
		<p>Your files have been upgraded to version <?php echo Cinnamon::getVersion(); ?>. Before using your newly installed version, you should upgrade your database as well.</p>
		<p class="buttons"><button onclick="location.href='upgrade.php?action=upgrade&url=<?php echo urlencode($_GET['url']); ?>';">OK, Continue &raquo;</button></p>
<?php endif; ?>
		</div>
	</div>
</body>
</html>
